﻿using System.Collections.Generic;

namespace wpControl1
{
    class ZoomStyleManager
    {
        /// <summary>
        /// The zoomstyle set for each image.
        /// </summary>
        internal List<Wallpaper.Style> Styles;

        /// <summary>
        /// Creates a new list of styles.
        /// </summary>
        internal void InitStyleStack()
        {
            this.Styles = new List<Wallpaper.Style>();
        }

        /// <summary>
        /// Saves the selected zoom style for the image.
        /// </summary>
        internal void SaveStyle()
        {
            // Get index of selected image
            int selectedImage = Program.ControlPanel.listBoxImages.SelectedIndex;
            // Get index of selected zoom style
            int selectedStyleIndex = Program.ControlPanel.dropdownStyle.SelectedIndex;
            // Make sure an image has been selected
            if (selectedImage != -1 && selectedStyleIndex != -1)
            {
                // Determine what style is selected
                Wallpaper.Style selectedStyle;
                if (Program.ControlPanel.dropdownStyle.SelectedValue.ToString() == Program.ControlPanel.StyleNames[0])
                {
                    selectedStyle = Wallpaper.Style.Stretched;
                }
                else if (Program.ControlPanel.dropdownStyle.SelectedValue.ToString() == Program.ControlPanel.StyleNames[1])
                {
                    selectedStyle = Wallpaper.Style.Tiled;
                }
                else if (Program.ControlPanel.dropdownStyle.SelectedValue.ToString() == Program.ControlPanel.StyleNames[2])
                {
                    selectedStyle = Wallpaper.Style.Centered;
                }
                else
                {
                    // default value
                    selectedStyle = 0;
                }
                // Save selected style into the style list
                this.Styles[selectedImage] = selectedStyle;
            }
        }

        /// <summary>
        /// Loads the zoom style for the selected image.
        /// </summary>
        internal void LoadStyle()
        {
            // Get index of selected image
            int selectedImage = Program.ControlPanel.listBoxImages.SelectedIndex;
            // Make sure an image is selected
            if (selectedImage != -1)
            {
                // Check which style is selected for the image
                if (this.Styles[selectedImage] == Wallpaper.Style.Stretched)
                {
                    Program.ControlPanel.dropdownStyle.SelectedIndex = 0;
                }
                else if (this.Styles[selectedImage] == Wallpaper.Style.Tiled)
                {
                    Program.ControlPanel.dropdownStyle.SelectedIndex = 1;
                }
                else if (this.Styles[selectedImage] == Wallpaper.Style.Centered)
                {
                    Program.ControlPanel.dropdownStyle.SelectedIndex = 2;
                }
            }
        }

        /// <summary>
        /// Gets the index of a zoom style.
        /// </summary>
        /// <param name="style">The style you want to get the matching index from.</param>
        /// <returns>The index of the given style. -1 if the argument is invalid.</returns>
        internal int GetStyleIndex(Wallpaper.Style style)
        {
            int index;
            if (style == Wallpaper.Style.Stretched)
            {
                index = 0;
            }
            else if (style == Wallpaper.Style.Tiled)
            {
                index = 1;
            }
            else if (style == Wallpaper.Style.Centered)
            {
                index = 2;
            }
            else
            {
                index = -1;
            }
            return index;
        }

        /// <summary>
        /// Gets the style based on a given index.
        /// </summary>
        /// <param name="index">The index of the style you want to get.</param>
        /// <returns>The style based on the given index. 0 if the argument is invalid.</returns>
        internal Wallpaper.Style GetStyle(int index)
        {
            Wallpaper.Style style;
            if (index == 0)
            {
                style = Wallpaper.Style.Stretched;
            }
            else if (index == 1)
            {
                style = Wallpaper.Style.Tiled;
            }
            else if (index == 2)
            {
                style = Wallpaper.Style.Centered;
            }
            else
            {
                style = 0;
            }
            return style;
        }
    }
}
